<?php
namespace App\Services\AnswerSurvey;
use App\Models\AnswerSurvey;
use App\Models\AnswerSurveyLog;
use Illuminate\Support\Facades\Auth;

/**
 * Created by PhpStorm.
 * User: 共赢岛
 * Date: 2024/3/28
 * Time: 14:09
 */

class AnswerSurveyService {

    public static function one($answer_id)
    {
        $answer_survey = AnswerSurvey::where('status', DEFAULT_STATUS_SUCCESS)->find($answer_id);

        $new_arr = $answer_survey->toArray();
        $new_arr['desc'] = $new_arr['content'];
        $new_arr['number'] = count($answer_survey->subject->toArray());

        foreach ($answer_survey->subject as $key => $value) {
            $subject = $value->toArray();
            $subject['type'] = answerTypeTitle($subject['type']);
            $subject['children'] = $value->option->toArray();

            $new_arr['questions'][$key] = $subject;
        }

        return $new_arr;
    }

    public static function create($param)
    {
        $user = Auth::user();

        foreach ($param->userAnswerList as $key => $value) {
            AnswerSurveyLog::create([
                'user_id' => $user->id ?? '',
                'answer_id' => $param->pkId,
                'subject_id' => $value->questionId,
                'option_id' => $value->questionType == 'QUESTION' ? '' : $value->userAnswer,
                'content' => $value->userAnswer
            ]);
        }

    }
}